<template>
<div class="app-container">
    <el-row>
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
        <el-col :span="6">
          <el-form-item label="品牌名称" prop="brandName">
            <el-input
            v-model="queryParams.brandName"
              placeholder="请输入品牌名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="分类" prop="classifyId" >
              <el-select v-model="queryParams.classifyId" size="small" style="width:240px" placeholder="请选择">
                <el-option
                  v-for="item in goodsOptions"
                  :key="item.classifyId"
                  :label="item.classifyName"
                  :value="item.classifyId"
                ></el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button style="margin-left:15px" type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
    </el-form>
    </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增品牌</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdds"
            >新增分类</el-button>
          </el-col>
        </el-row>

    <el-table v-loading="loading" :data="goodsList">
      <el-table-column label="品牌名称" prop="brandName" align="center" />
      <el-table-column label="分类" prop="classifyName" align="center"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
              <el-button
                size="small"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.brandId)"
              >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

 <!-- 新增品牌 -->
    <el-dialog :title="title" :visible.sync="open"  width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="15" :offset="3">
            <el-form-item label="品牌名" prop="brandName" >
              <el-input v-model="form.brandName" placeholder="请输入品牌名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="15" :offset="3">
            <el-form-item label="分类" prop="classifyId">
              <el-select v-model="form.classifyId" style="width:100%" placeholder="请选择">
                <el-option
                  v-for="item in goodsOptions"
                  :key="item.classifyId"
                  :label="item.classifyName"
                  :value="item.classifyId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="15" :offset="3">
            <el-form-item label="详细说明" prop="brandDetail" >
              <el-input v-model="form.brandDetail" type="textarea" :rows="4" placeholder="请输入详细说明"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">新 增</el-button>
      </div>
    </el-dialog>

    <!-- 新增分类 -->
    <el-dialog :title="title" :visible.sync="opens" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="15" :offset="3">
            <el-form-item label="分类名" prop="classifyName">
              <el-input v-model="form.classifyName" placeholder="请输入新的分类名"/>
            </el-form-item>
          </el-col>
          <el-col :span="15" :offset="3">
            <el-form-item label="详细说明" prop="classifyDetail" >
              <el-input v-model="form.classifyDetail" type="textarea" :rows="4" placeholder="请输入详细说明"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForms">新 增</el-button>
      </div>
    </el-dialog>

</div>
</template>
<script>
import { getBrand, addBrand, getClassify, addClassify, delBrand } from "@/api/shop/goods";

export default {
    name:"Brand",
    data() {
        return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      goodsList: null,
      // 表单参数
      form: {},

      ids: [],
      //对话框
      open: false,
      opens: false,
      title:'',

      //分类选项
      goodsOptions: [],

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandName: undefined,
        goodsClassify: undefined,
        classifyId: undefined,
        classifyIds: [],
        status: undefined,
      },
      // 表单校验
      rules: {
        brandName: [
          { required: true, message: "品牌名称不能为空", trigger: "blur" }
        ],
        goodsClassify: [
          { required: true, message: "品牌分类不能为空", trigger: "blur" }
        ],
        classifyDetail: [
          { required: true, message: "详细说明不能为空", trigger: "blur" }
        ],
        brandDetail: [
          { required: true, message: "详细说明不能为空", trigger: "blur" }
        ],
      }
        };
    },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getClassify();
  },
  methods:{
    /** 查询品牌列表 */
    getList() {
      this.loading = true;
      getBrand(this.addDateRange(this.queryParams)).then(
        response => {
          this.goodsList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    getClassify(){
      getClassify().then(response =>{
        this.goodsOptions = response.data;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        classifyId: undefined,
        brandName: undefined,
        goodsClassify: undefined,
        classifyIds:[],
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getClassify();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getClassify().then(response => {
        this.classifyOptions = response.classifys;
        this.open = true;
        this.title = "新增品牌信息";
      });
    },
    handleAdds() {
      this.reset();
        this.opens = true;
        this.title = "新增分类信息";
    },
    // 删除
    handleDelete(brandId) {
      const brandIds = brandId || this.ids;
      this.$confirm('是否确认删除该品牌?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBrand(brandIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },

    //新增品牌提交
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.brandName != undefined) {
            addBrand(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    //新增分类提交
    submitForms: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.classifyName != undefined) {
            addClassify(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.opens = false;
              this.getClassify();
            });
          }
        }
      });
    },

  }
}
</script>

<style lang="less" scoped>

</style>
